<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class UlistController extends Controller
{
    //
      public function index(Request $request)
    {
    	//保存搜索条件
    	$where = [];
    	//实例化要操作的表
    	$db = DB::table("user");
    	//判断有没有要搜索name字段的值
    	if($request->has("username")){
            //获取要搜索的name字段的值
            $username = $request->input("username");
            //添加where模糊匹配条件
    		$db->where("username","like","%{$username}%");
    		$where['username'] = $username;
    	}
    	//分页
    	$list=$db->paginate(1);

    	return view ("admin.user.Ulist",["list"=>$list,"where"=>$where]);
    }
    public function destroy($id)
    {
    	$row = DB::table("user")->where("id",$id)->delete();
    	if($row>0){
    		return redirect("admin/Ulist");
    	}
    }
    public function edit($id)
    {
        $user = DB::table("user")->where("id",$id)->first();
        $data=$user->state=='0'?'1':'0';
        $update=array('state'=>$data);
        $row=DB::table('user')->where('id',$id)->update($update);
        if ($row>0) {
            return redirect('admin/Ulist');
        }
    }
}
